package com.zhuoshi.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.zhuoshi.entity.Project;
import com.zhuoshi.entity.ProjectCustom;
import com.zhuoshi.entity.Unit;
import com.zhuoshi.entity.UnitCustom;
import com.zhuoshi.entity.User;
import com.zhuoshi.entity.UserCustom;
import com.zhuoshi.service.ProjectService;
import com.zhuoshi.service.UserService;

@Controller
@RequestMapping("project")
public class ProjectController {
	@Autowired
	private ProjectService  projectService;
	
	@Autowired
	private UserService userService;
	
//	查询所有项目(小伟)
	@RequestMapping("selectAllProjectYao")
	public ModelAndView selectAllProjectYao() throws Exception{
		List<Project> projects=projectService.selectAllProjectYao();
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.addObject("projects",projects);
		modelAndView.setViewName("/Admin/second/openPanManager.jsp");
		return modelAndView;
	}
	
	@RequestMapping("selectAllProjectLiu")
	public ModelAndView selectAllProjectCustomLiu(ProjectCustom projectCustom) throws Exception{
		List<ProjectCustom> projects=projectService.selectAllProjectCustomLiu(projectCustom.getName());
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.addObject("projects",projects);
		modelAndView.setViewName("/Admin/projectManager.jsp");
		return modelAndView;
	}
	
	
	@RequestMapping("deleteProjectByIdLiu")
	public String deleteProjectByIdLiu(String id) {
		String []ids=id.split(",");
		int a=0;
		for(int i=0;i<ids.length;i++) {
			int rows=projectService.deleteProjectByIdLiu(Integer.parseInt(ids[i]));
			a+=rows;
		}		
		if(a==ids.length) {
			return "redirect:selectAllProjectLiu.action";
		}else {
			return "redirect:error.jsp";
		}
	}
	
	//添加项目信息
		@RequestMapping("addProjectMessageLiu")
		public ModelAndView addUnitMessageLiu() throws Exception {
			List<UserCustom> users=userService.selectUserAllYao("");
			ModelAndView modelAndView=new ModelAndView();
			modelAndView.addObject("users", users);
			modelAndView.setViewName("/Admin/addProject.jsp");
			return modelAndView;
		}
		
		//确认添加
		@RequestMapping("addProjectConfirmLiu")
		public String addProjectConfirmLiu(Project Project) {
			int rows=projectService.addProjectConfirmLiu(Project);
			if(rows>0) {
				return "redirect:selectAllProjectLiu.action";
			}else {
				return "redirect:error.jsp";
			}
		}
		
		//修改信息
		@RequestMapping("updateProjectMessageLiu")
		public ModelAndView updateProjectMessageLiu(int id) throws Exception {
			List<UserCustom> users=userService.selectUserAllYao("");			
			ProjectCustom project=projectService.selectAllProjectCustomByIdLiu(id);
			ModelAndView modelAndView=new ModelAndView();
			modelAndView.addObject("users", users);
			modelAndView.addObject("project", project);
			modelAndView.setViewName("/Admin/updateProject.jsp");
			return modelAndView;
		}
		
		//确认修改信息
		@RequestMapping("updateProjectConfirmLiu")
		public String updateProjectConfirmLiu(Project project) {
			int rows=projectService.updateProjectConfirmLiu(project);
			if(rows>0) {
				return "redirect:selectAllProjectLiu.action";
			}else {
				return "redirect:error.jsp";
			}
		}
}
